package com.hzjh.base.http.interceptor

import okhttp3.Headers
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response

/**
 *  拦截后 设置请求 token
 **/
class TokenInterceptor : Interceptor {

    companion object {
        private var headers : (() -> Headers)? = null
        fun setHeaders(myHeaders: (() -> Headers)?){
            headers=myHeaders
        }
        fun getHeaders() : (() -> Headers)? {
            return headers
        }
    }

    override fun intercept(chain: Interceptor.Chain): Response {
        val originalRequest = chain.request()
        var tokenRequest: Request? = null
        tokenRequest = originalRequest.newBuilder()
            .headers(headers = getHeaders()?.invoke()?: Headers.Builder().build())
            .build()

        return chain.proceed(tokenRequest)
    }

}